﻿using System;
using System.Data;
using System.Windows.Forms;
using ProgramMain.Classifiers;
using ProgramMain.Database;

namespace ProgramMain.OpticNet
{
    public partial class CableBoxDetail : ProgramMain.Framework.BaseModalDetailCtl
    {
        private CableBoxKinds m_CableBoxKind;

        public CableBoxDetail(CableBoxKinds CableBoxKind)
        {
            InitializeComponent();

            m_CableBoxKind = CableBoxKind;

            switch (m_CableBoxKind)
            {
                case CableBoxKinds.CableBox:
                    labelCaption.Text = "Муфта";
                    layoutControlItem1.Text = "Тип муфты:";
                    break;
                case CableBoxKinds.CableBoxLink:
                    labelCaption.Text = "Соединитель муфт";
                    layoutControlItem1.Text = "Тип соединителя:";
                    break;
                default:
                    labelCaption.Text = "Муфта";
                    layoutControlItem1.Text = "Тип муфты:";
                    break;

            }
        }

        protected override void InitializeDataControls()
        {
            this.curveElementTypesTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.curveElementTypesTableAdapter.Fill(this.simpleOpticNetDB.CurveElementTypes, Convert.ToInt32(m_CableBoxKind));

            if (ShowMode == ShowModeTypes.Add)
            try
            {
                SimpleOpticNetDB.CableBoxesRow row = (SimpleOpticNetDB.CableBoxesRow)CurrentRowView.Row;
                if (this.curveElementTypesBindingSource.Count > 0)
                {
                    this.curveElementTypesBindingSource.MoveFirst();
                    SimpleOpticNetDB.CurveElementTypesRow curveElementTypesRow = (SimpleOpticNetDB.CurveElementTypesRow)((DataRowView)this.curveElementTypesBindingSource.Current).Row;
                    row.CurveElementTypeID = curveElementTypesRow.ID;
                }
            }
            catch{}
        }

        protected override void BindDataControls()
        {
        //    if(! ((SimpleOpticNetDB.CableBoxesRow)((DataRowView)dataSource.Current).Row).IsCaptionNull())
            this.txtEditCaption.DataBindings.Add(new Binding("Text", this.dataSource, "Caption"));
        
            this.lookupCurveElementTypeID.DataBindings.Add(new Binding("EditValue", this.dataSource, "CurveElementTypeID"));
            this.memoEditDesc.DataBindings.Add(new Binding("Text", this.dataSource, "Description"));
            this.lookupCurveElementTypeID.Enabled = ShowMode == ShowModeTypes.Add;

            if (string.IsNullOrEmpty(this.txtEditCaption.Text.Trim()) && dataSource.Current != null)
            {
                string cap = "";
                switch(m_CableBoxKind)
                {
                    case CableBoxKinds.CableBox:
                        cap = "Муфта 1";
                        break;
                    case CableBoxKinds.CableBoxLink:
                        cap = "Соединитель 1";
                        break;
                   default:
                        cap = "Муфта 1";
                        break;

                }
                ((SimpleOpticNetDB.CableBoxesRow) ((DataRowView) dataSource.Current).Row).Caption = cap;
            }
        }
    }
}
